﻿using MineMES.Domain.Entity;
using Microsoft.Extensions.Logging;
using MineMES.Application.DTO;
using MineMES.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using IDapperRepository;
using System.Threading.Tasks;

namespace MineMES.Application
{
    public class AccountApps
    {
        private readonly ILogger _logger;
        private readonly MineMESDbContext _context;
        private readonly IDapperBaseRepository<Account> _dapperBaseRepository;
        private Result result = null;
        public AccountApps(ILogger<AccountApps> logger, MineMESDbContext Context, IDapperBaseRepository<Account> dapperBaseRepository)
        {
            _logger = logger;
            _context = Context;
            _dapperBaseRepository = dapperBaseRepository;
        }

        public async Task<IEnumerable<Account>> Load()
        {
            //return _context.Accounts.ToList();
            string sql = "select * from Accounts";
            var lst = await _dapperBaseRepository.QueryList(sql);
            return lst;
        } 

        public Account GetById(string accoutId)
        {
            return _context.Accounts.FirstOrDefault(x => x.Id == accoutId);
        }

        public void Add(Account account)
        {
            _context.Add<Account>(account);
        }

        public void Update(Account account){
            _context.Update<Account>(account);
        }

        public Result Delete(string[] accountIds)
        {
            _context.RemoveRange(_context.Accounts.Where(x => accountIds.Contains(x.Id)));
            result.IsSuccess = true;
            result.Code = 200;
            result.Message = "删除成功";
            result.Data = null;
            return result;
        }
    }
}
